package cn.lbd.datastructure;

public class ReserveLinkedList {
    public static void reserve(HeroNode head){
        //如果链表长度为0或1，则不需要逆转
        if (head.next == null && head.next.next == null){
            return;
        }
        //head链表的辅助指针
        HeroNode temp = head.next;
        //逆转后的头节点
        HeroNode reserveHead = new HeroNode(0,"","");
        //存储原链表临时的下一节点数据
        HeroNode next = null;
        while (temp !=null){//最后一个节点也移入了新链表
            next = temp.next;
            //加入到新链表最前端
            temp.next = reserveHead.next;
            reserveHead.next = temp;
            //temp = temp.next
            temp = next;
        }
        //将旧链表头节点指向新链表的第一个节点
        head.next = reserveHead.next;
    }

    public static void main(String[] args) {
        HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
        HeroNode heroNode4 = new HeroNode(4, "公孙胜", "入云龙");

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.add(heroNode1);
        singleLinkedList.add(heroNode2);
        singleLinkedList.add(heroNode3);
        singleLinkedList.add(heroNode4);
        singleLinkedList.list();
        System.out.println("=======================");
        reserve(singleLinkedList.getHead());
        singleLinkedList.list();
    }
}
